package com.everest.tribalapp;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Dering_Pengelola_3 extends Activity implements OnClickListener {

	private Spinner derp1_provinsi;
	private Spinner derp1_kubupaten;
	private Spinner derp1_kecamaten;
	private Spinner derp1_desa;
	private EditText derp1_kpu1;
	private EditText derp1_kpu2;
	private EditText derp1_catatan;
	private Button next;
	private TextView dering_peng_q3_1a;
	private TextView dering_peng_q3_2;
	private TextView dering_peng_q3_3a;
	private TextView dering_peng_q3_4a;
	private TextView dering_peng_q3_4d;
	private CheckBox checkBox_1a3;
	private CheckBox checkBox_1b3;
	private CheckBox checkBox_1c3;
	private CheckBox checkBox_1d3;
	private CheckBox checkBox_3a3;
	private CheckBox checkBox_3b3;
	private CheckBox checkBox_3c3;
	private CheckBox checkBox_3d3;
	private CheckBox checkBox_4a3;
	private CheckBox checkBox_4b3;
	private CheckBox checkBox_4c3;
	private EditText dering_f3_ans_2;
	private EditText dering_f3_ans_4d;
	protected String selectetdVal;
	protected boolean[] selectVal1 = { false, false, false, false };
	protected boolean[] selectVal3 = { false, false, false, false };
	protected boolean[] selectVal4 = { false, false, false };
	private Button prev;
	protected String message;
	private String[] choice;
	private int flag = 0;
	private int selectvalue = 0;
	private int id = 0;
	private String value;
	private CharSequence[] MSG_VALUES_1 = { "Melalui proses seleksi","Atas kemauan sendiri dengan pengajuan", "Ditunjuk langsung oleh penyedia jasa fastel ","Ditunjuk langsung oleh desa " };
	private CharSequence[] MSG_VALUES_2 = { "YA" , "TIDAK" };
	private CharSequence[] MSG_VALUES_3 = { "KTP","Photo", "Pendidikan minmal SLTA/sederajat ","Domisili setempat" };
	private CharSequence[] MSG_VALUES_4 = { "Ingin mencoba bisnis telekomunikasi","Ditunjuk", "Lainnya, Tuliskan" };
	

	@Override
	public void onBackPressed() {

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Dering_Pengelola_3.this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dering_pengelola_3);

		initialization();

		prev.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent in = new Intent(getApplicationContext(),
						Dering_Pengelola_2.class);
				startActivity(in);
				Dering_Pengelola_3.this.finish();
			}
		});

		next.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent in = new Intent(getApplicationContext(),
						Dering_Pengelola_4.class);
				startActivity(in);
				Dering_Pengelola_3.this.finish();
			}
		});

	}

	private void initialization() {

		dering_peng_q3_1a = (TextView) findViewById(R.id.dering_peng_q3_1a);
		dering_peng_q3_2 = (TextView) findViewById(R.id.dering_peng_q3_2);
		dering_peng_q3_3a = (TextView) findViewById(R.id.dering_peng_q3_3a);
		dering_peng_q3_4a = (TextView) findViewById(R.id.dering_peng_q3_4a);
		dering_peng_q3_4d = (TextView) findViewById(R.id.dering_peng_q3_4d);
		dering_f3_ans_2 = (EditText) findViewById(R.id.dering_f3_ans_2);
		dering_f3_ans_4d = (EditText) findViewById(R.id.dering_f3_ans_4d);
		checkBox_1a3=(CheckBox)findViewById(R.id.checkBox_1a3);
		checkBox_1b3=(CheckBox)findViewById(R.id.checkBox_1b3);
		checkBox_1c3=(CheckBox)findViewById(R.id.checkBox_1c3);
		checkBox_1d3=(CheckBox)findViewById(R.id.checkBox_1d3);
		checkBox_3a3=(CheckBox)findViewById(R.id.checkBox_3a3);
		checkBox_3b3=(CheckBox)findViewById(R.id.checkBox_3b3);
		checkBox_3c3=(CheckBox)findViewById(R.id.checkBox_3c3);
		checkBox_3d3=(CheckBox)findViewById(R.id.checkBox_3d3);
		checkBox_4a3=(CheckBox)findViewById(R.id.checkBox_4a3);
		checkBox_4b3=(CheckBox)findViewById(R.id.checkBox_4b3);
		checkBox_4c3=(CheckBox)findViewById(R.id.checkBox_4c3);
		prev = (Button) findViewById(R.id.previous);
		next = (Button) findViewById(R.id.next);
		dering_peng_q3_1a.setOnClickListener(this);
		dering_peng_q3_2.setOnClickListener(this);
		dering_peng_q3_3a.setOnClickListener(this);
		dering_peng_q3_4a.setOnClickListener(this);
		dering_peng_q3_4d.setOnClickListener(this);
		dering_f3_ans_2.setOnClickListener(this);
		checkBox_1a3.setOnClickListener(this);
		checkBox_1b3.setOnClickListener(this);
		checkBox_1c3.setOnClickListener(this);
		checkBox_1d3.setOnClickListener(this);
		checkBox_3a3.setOnClickListener(this);
		checkBox_3b3.setOnClickListener(this);
		checkBox_3c3.setOnClickListener(this);
		checkBox_3d3.setOnClickListener(this);
		checkBox_4a3.setOnClickListener(this);
		checkBox_4b3.setOnClickListener(this);
		checkBox_4c3.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		if (v.getId() == dering_peng_q3_1a.getId()) {
			value = getResources().getString(R.string.dering_peng_q3_1a);
			toast_msg();
		} else if (v.getId() == dering_peng_q3_2.getId()) {
			value = getResources().getString(R.string.dering_peng_q3_2);
			toast_msg();
		} else if (v.getId() == dering_peng_q3_3a.getId()) {
			value = getResources().getString(R.string.dering_peng_q3_3a);
			toast_msg();
		} else if (v.getId() == dering_peng_q3_4a.getId()) {
			value = getResources().getString(R.string.dering_peng_q3_4a);
			toast_msg();
		} else if (v.getId() == dering_peng_q3_4d.getId()) {
			value = getResources().getString(R.string.dering_peng_q3_4d);
			toast_msg();
		} else if (v.getId() == dering_f3_ans_2.getId()) {
			msg_dering_f3_ans_2();
		}
		else if(v.getId()== checkBox_1a3.getId()){
			checkBox_1a3.setChecked(false);
			msg_dering_f3_ans_1();
		}
		else if(v.getId()==checkBox_1b3.getId())
		{
			checkBox_1b3.setChecked(false);
			msg_dering_f3_ans_1();
		}
		else if(v.getId()==checkBox_1c3.getId())
		{
			checkBox_1c3.setChecked(false);
			msg_dering_f3_ans_1();
		}
		else if(v.getId()==checkBox_1d3.getId())
		{
			checkBox_1d3.setChecked(false);
			msg_dering_f3_ans_1();
		}
		else if(v.getId()==checkBox_3a3.getId())
		{
			checkBox_3a3.setChecked(false);
			msg_dering_f3_ans_3();
		}
		else if(v.getId()==checkBox_3b3.getId())
		{
			checkBox_3b3.setChecked(false);
			msg_dering_f3_ans_3();
		}
		else if(v.getId()==checkBox_3c3.getId())
		{
			checkBox_3c3.setChecked(false);
			msg_dering_f3_ans_3();
		}
		else if(v.getId()==checkBox_3d3.getId())
		{
			checkBox_3d3.setChecked(false);
			msg_dering_f3_ans_3();
		}
		else if(v.getId()==checkBox_4a3.getId())
		{
			checkBox_4a3.setChecked(false);
			msg_dering_f3_ans_4();
		}
		else if(v.getId()==checkBox_4b3.getId())
		{
			checkBox_4b3.setChecked(false);
			msg_dering_f3_ans_4();
		}
		else if(v.getId()==checkBox_4c3.getId())
		{
			checkBox_4c3.setChecked(false);
			msg_dering_f3_ans_4();
		}
	}

	public void toast_msg() {
		LayoutInflater inflater = getLayoutInflater();

		View layout = inflater.inflate(R.layout.custom_toast,
				(ViewGroup) findViewById(R.id.custom_toast_layout_id));
		TextView text = (TextView) layout.findViewById(R.id.text);
		text.setText(value);
		Toast toast = new Toast(getApplicationContext());
		toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
		toast.setDuration(Toast.LENGTH_LONG);
		toast.setView(layout);
		toast.show();
	}

	private void msg_dering_f3_ans_2() {
	     Builder builder = new AlertDialog.Builder(this);
	     builder.setTitle("Select");
	     builder.setSingleChoiceItems(MSG_VALUES_2, -1, new DialogInterface.OnClickListener() {
	       

		@Override
	       public void onClick(DialogInterface dialog, int which) {
			 dering_f3_ans_2.setText(""+(which+1));
	         dialog.dismiss();
	         
	       }
	     });
	     builder.setNegativeButton("cancel",
	         new DialogInterface.OnClickListener() {
	           @Override
	           public void onClick(DialogInterface dialog, int which) {
	             dialog.dismiss();
	           }
	         });
	     AlertDialog alert = builder.create();
	     alert.show();
	   }
	
/*	private void msg_dering_f3_ans_4d() {
	     Builder builder = new AlertDialog.Builder(this);
	     builder.setTitle("Select");
	     builder.setSingleChoiceItems(MSG_VALUES_4, -1, new DialogInterface.OnClickListener() {
	       

		@Override
	       public void onClick(DialogInterface dialog, int which) {
			 dering_f3_ans_2.setText(""+(which+1));
	         dialog.dismiss();
	         
	       }
	     });
	     builder.setNegativeButton("cancel",
	         new DialogInterface.OnClickListener() {
	           @Override
	           public void onClick(DialogInterface dialog, int which) {
	             dialog.dismiss();
	           }
	         });
	     AlertDialog alert = builder.create();
	     alert.show();
	   }*/

	private void msg_dering_f3_ans_1()
	   {
		     Builder builder = new AlertDialog.Builder(this);
		     builder.setTitle("Answer");
		     builder.setMultiChoiceItems(MSG_VALUES_1, selectVal1,
		         new DialogInterface.OnMultiChoiceClickListener()
		     {
		           @Override
		           public void onClick(DialogInterface dialog, int which,
		               boolean isChecked) 
		           {
		             selectVal1[which] = isChecked;
		           }
		         });
		     builder.setPositiveButton("Set", new DialogInterface.OnClickListener() {
		       @Override
		       public void onClick(DialogInterface dialog, int which) {
		         for (int i = 0; i <MSG_VALUES_1.length; i++) 
		         {
		           if (selectVal1[i]) 
		           {
		              if(i==0)
		 				{
		            	 
		            	  checkBox_1a3.setChecked(true);
	 					}
		 				else if(i==1)
		 				{
		 					checkBox_1b3.setChecked(true);
		 				}
		 				else if(i==2)
		 				{
		 					checkBox_1c3.setChecked(true);
		 				}
		 				else if(i==3)
		 				{
		 					checkBox_1d3.setChecked(true);
		 				}
		           }
		           else
		           {
		        	   if(i==0)
		 				{
		            	 
		        		   checkBox_1a3.setChecked(false);
	 					}
		 				else if(i==1)
		 				{
		 					checkBox_1b3.setChecked(false);
		 				}
		 				else if(i==2)
		 				{
		 					checkBox_1c3.setChecked(false);
		 				}
		 				else if(i==3)
		 				{
		 					checkBox_1d3.setChecked(false);
		 				}
		 				
		           }
		         }
		        }
		     });
		     builder.setNegativeButton("cancel",
		         new DialogInterface.OnClickListener() {
		           @Override
		           public void onClick(DialogInterface dialog, int which) {
		             dialog.dismiss();
		           }
		         });
		     AlertDialog alert = builder.create();
		     alert.show();
		   } 
	
	private void msg_dering_f3_ans_3()
	   {
		     Builder builder = new AlertDialog.Builder(this);
		     builder.setTitle("Answer");
		     builder.setMultiChoiceItems(MSG_VALUES_3, selectVal3,
		         new DialogInterface.OnMultiChoiceClickListener()
		     {
		           @Override
		           public void onClick(DialogInterface dialog, int which,
		               boolean isChecked) 
		           {
		             selectVal3[which] = isChecked;
		           }
		         });
		     builder.setPositiveButton("Set", new DialogInterface.OnClickListener() {
		       @Override
		       public void onClick(DialogInterface dialog, int which) {
		         for (int i = 0; i <MSG_VALUES_1.length; i++) 
		         {
		           if (selectVal1[i]) 
		           {
		              if(i==0)
		 				{
		            	 
		            	  checkBox_3a3.setChecked(true);
	 					}
		 				else if(i==1)
		 				{
		 					checkBox_3b3.setChecked(true);
		 				}
		 				else if(i==2)
		 				{
		 					checkBox_3c3.setChecked(true);
		 				}
		 				else if(i==3)
		 				{
		 					checkBox_3d3.setChecked(true);
		 				}
		           }
		           else
		           {
		        	   if(i==0)
		 				{
		            	 
		        		   checkBox_3a3.setChecked(false);
	 					}
		 				else if(i==1)
		 				{
		 					checkBox_3b3.setChecked(false);
		 				}
		 				else if(i==2)
		 				{
		 					checkBox_3c3.setChecked(false);
		 				}
		 				else if(i==3)
		 				{
		 					checkBox_3d3.setChecked(false);
		 				}
		 				
		           }
		         }
		        }
		     });
		     builder.setNegativeButton("cancel",
		         new DialogInterface.OnClickListener() {
		           @Override
		           public void onClick(DialogInterface dialog, int which) {
		             dialog.dismiss();
		           }
		         });
		     AlertDialog alert = builder.create();
		     alert.show();
		   } 
	private void msg_dering_f3_ans_4()
	   {
		     Builder builder = new AlertDialog.Builder(this);
		     builder.setTitle("Answer");
		     builder.setMultiChoiceItems(MSG_VALUES_4, selectVal4,
		         new DialogInterface.OnMultiChoiceClickListener()
		     {
		           @Override
		           public void onClick(DialogInterface dialog, int which,
		               boolean isChecked) 
		           {
		             selectVal4[which] = isChecked;
		           }
		         });
		     builder.setPositiveButton("Set", new DialogInterface.OnClickListener() {
		       @Override
		       public void onClick(DialogInterface dialog, int which) {
		         for (int i = 0; i <MSG_VALUES_4.length; i++) 
		         {
		           if (selectVal4[i]) 
		           {
		              if(i==0)
		 				{
		            	 
		            	  checkBox_4a3.setChecked(true);
	 					}
		 				else if(i==1)
		 				{
		 					checkBox_4b3.setChecked(true);
		 				}
		 				else if(i==2)
		 				{
		 					checkBox_4c3.setChecked(true);
		 				}
		           }
		           else
		           {
		        	   if(i==0)
		 				{
		            	 
		        		   checkBox_4a3.setChecked(false);
	 					}
		 				else if(i==1)
		 				{
		 					checkBox_4b3.setChecked(false);
		 				}
		 				else if(i==2)
		 				{
		 					checkBox_4c3.setChecked(false);
		 				}
		           }
		         }
		        }
		       
		     });
		     builder.setNegativeButton("cancel",
		         new DialogInterface.OnClickListener() {
		           @Override
		           public void onClick(DialogInterface dialog, int which) {
		             dialog.dismiss();
		           }
		         });
		     AlertDialog alert = builder.create();
		     alert.show();
		   } 
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		SharedPreferences preferences = getSharedPreferences(
				"dering_f3_details", MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();
		editor.putBoolean("3_1a",checkBox_1a3.isChecked());
		editor.putBoolean("3_1b",checkBox_1b3.isChecked());
		editor.putBoolean("3_1c",checkBox_1c3.isChecked());
		editor.putBoolean("3_1d",checkBox_1d3.isChecked());
		editor.putString("3_2", dering_f3_ans_2.getText().toString().trim());
		editor.putBoolean("3_3a",checkBox_3a3.isChecked());
		editor.putBoolean("3_3b",checkBox_3b3.isChecked());
		editor.putBoolean("3_3c",checkBox_3c3.isChecked());
		editor.putBoolean("3_3d",checkBox_3d3.isChecked());
		editor.putBoolean("3_4a",checkBox_4a3.isChecked());
		editor.putBoolean("3_4b",checkBox_4b3.isChecked());
		editor.putBoolean("3_4c",checkBox_4c3.isChecked());
		editor.putString("3_4d2", dering_f3_ans_4d.getText().toString().trim());
		editor.commit();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		SharedPreferences preferences = getSharedPreferences(
				"dering_f3_details", MODE_PRIVATE);
		dering_f3_ans_2.setText(preferences.getString("3_2", ""));
		dering_f3_ans_4d.setText(preferences.getString("3_4d2", ""));
		checkBox_1a3.setChecked(preferences.getBoolean("3_1a",false));
		checkBox_1b3.setChecked(preferences.getBoolean("3_1b",false));
		checkBox_1c3.setChecked(preferences.getBoolean("3_1c",false));
		checkBox_1d3.setChecked(preferences.getBoolean("3_1d",false));
		checkBox_3a3.setChecked(preferences.getBoolean("3_3a",false));
		checkBox_3b3.setChecked(preferences.getBoolean("3_3b",false));
		checkBox_3c3.setChecked(preferences.getBoolean("3_3c",false));
		checkBox_3d3.setChecked(preferences.getBoolean("3_3d",false));
		checkBox_4a3.setChecked(preferences.getBoolean("3_4a", false));
		checkBox_4b3.setChecked(preferences.getBoolean("3_4b", false));
		checkBox_4c3.setChecked(preferences.getBoolean("3_4c", false));
	}
}
